package com.rn.ddd.infrastructure.repository.impl;

import com.rn.ddd.domain.merchant.entity.Merchant;
import com.rn.ddd.domain.merchant.entity.Store;
import com.rn.ddd.domain.merchant.repository.MerchantRepository;
import com.rn.ddd.infrastructure.repository.converter.MerchantDataConverter;
import com.rn.ddd.infrastructure.repository.converter.StoreDataConverter;
import com.rn.ddd.infrastructure.repository.mapper.MerchantMapper;
import com.rn.ddd.infrastructure.repository.mapper.StoreMapper;
import com.rn.ddd.infrastructure.repository.po.MerchantPO;
import com.rn.ddd.infrastructure.repository.po.StorePO;
import com.rn.ddd.infrastructure.util.CopyUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Repository
public class MerchantRepositoryImpl implements MerchantRepository {

    @Resource
    private MerchantMapper merchantMapper;
    @Resource
    private StoreMapper storeMapper;

    @Override
    public int checkMerchantName(String merchantName, Long[] exclude) {
        return merchantMapper.checkMerchantName(merchantName, exclude);
    }

    @Override
    public Merchant getById(long id) {
        MerchantPO merchantPo = merchantMapper.selectById(id);
        return CopyUtils.copy(merchantPo, Merchant.class);
    }

    @Override
    public boolean saveOrUpdate(Merchant merchant) {
        // DO 转 PO
        MerchantPO merchantPo = MerchantDataConverter.toData(merchant);
        if (merchantPo == null) {
            return false;
        }
        // 保存
        if (merchantPo.getId() != null) {
            // 保存商户
            if (merchantMapper.insert(merchantPo) != 1) {
                // 日志是否打印信息全量，需根据数据隐私、性能等方面评估，尤其是打印的对象较大时可能会导致严重性能问题
                log.error("saveOrUpdate: persistence merchantPo error.merchantPo={}", merchantPo);
                return false;
            }
            // 保存门店
            List<Store> stores = merchant.getStores();
            if (!CollectionUtils.isEmpty(stores)) {
                for (Store store : stores) {
                    StorePO storePo = StoreDataConverter.toData(store);
                    if (storePo == null || storeMapper.insert(storePo) != 1) {
                        log.error("saveOrUpdate: persistence storePo error.storePo={}", storePo);
                        return false;
                    }
                }
            }
        }
        // 更新
        else {
            // 更新商户
            if (merchantMapper.updateById(merchantPo) != 1) {
                log.error("saveOrUpdate: update merchantPo error.merchantPo={}", merchantPo);
                return false;
            }
            // 更新门店
            List<Store> stores = merchant.getStores();
            if (!CollectionUtils.isEmpty(stores)) {
                for (Store store : stores) {
                    StorePO storePo = StoreDataConverter.toData(store);
                    if (storePo == null || storeMapper.updateById(storePo) != 1) {
                        log.error("saveOrUpdate: persistence storePo error.storePo={}", storePo);
                        return false;
                    }
                }
            }
        }
        return true;
    }

}
